/*
 * DivisionsDao.java
 *
 * Created on November 9, 2008, 7:36 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package jollylengkono.skripsi.dao;

import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import jollylengkono.skripsi.iface.Crud;
import jollylengkono.skripsi.model.Divisions;

/**
 *
 * @author JollkY
 */
public class DivisionsDao implements Crud {
    
    private int state;
    private Divisions division;
    private List divisions;
    private EntityManagerFactory emf = Persistence.createEntityManagerFactory("kmPU");
    private EntityManager em = emf.createEntityManager();
    
    /** Creates a new instance of DivisionsDao */
    public DivisionsDao() {
    }

    
    public int create(Object obj) {
        try {
            em.getTransaction().begin();
            em.persist(obj);
            em.getTransaction().commit();

            this.state = 1;
        } catch (Exception ex) {
            ex.printStackTrace();
            em.getTransaction().rollback();

            this.state = 0;
        } finally {
            em.close();
        }
        return this.state;
    }

    
    public List<Object> getList() {
        divisions = new ArrayList();
        try {
            em.getTransaction().begin();
            divisions = em.createQuery("SELECT division FROM Divisions division").getResultList();
            em.getTransaction().commit();
        } catch (Exception ex) {
            ex.printStackTrace();
            em.getTransaction().rollback();
        } finally {
            em.close();
        }
        return divisions;
    }

    
    public Object read(long id) {
        division = new Divisions();
        try {
            em.getTransaction().begin();
            division = em.find(Divisions.class, id);
            em.getTransaction().commit();
        } catch (Exception ex) {
            ex.printStackTrace();
            em.getTransaction().rollback();
        } finally {
            em.close();
        }
        return division;
    }

    
    public int update(Object obj) {
        division = new Divisions();
        try {
            Divisions objDiv = (Divisions) obj;
            em.getTransaction().begin();
            division = em.find(Divisions.class, objDiv.getId());
            division.setDivisionManager(objDiv.getDivisionManager());
            division.setDivisionName(objDiv.getDivisionName());
            division.setListWorker(objDiv.getListWorker());
            em.getTransaction().commit();

            this.state = 1;
        } catch (Exception ex) {
            ex.printStackTrace();
            em.getTransaction().rollback();

            this.state = 0;
        } finally {
            em.close();
        }
        return this.state;
    }

    
    public int delete(long id) {
        division = new Divisions();
        try {
            em.getTransaction().begin();
            division = em.find(Divisions.class, id);
            em.remove(division);
            em.getTransaction().commit();

            this.state = 1;
        } catch (Exception ex) {
            ex.printStackTrace();
            em.getTransaction().rollback();

            this.state = 0;
        } finally {
            em.close();
        }
        return state;
    }
    
}
